package com.risk.kernel.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.risk.kernel.domain.TkTaskCase;
import com.risk.kernel.domain.TkTaskCaseInfo;
import com.risk.kernel.mapper.TkTaskCaseInfoMapper;
import com.risk.kernel.mapper.TkTaskCaseMapper;
import com.risk.kernel.service.TkTaskCaseInfoServiceI;
import com.risk.kernel.utils.JSONUtils;
import com.risk.kernel.vo.TkTaskCaseInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: Liwen
 * @createTime: 2025/1/7
 * @description:
 */
@Service
public class TkTaskCaseInfoServiceImpl implements TkTaskCaseInfoServiceI {
    @Autowired
    private TkTaskCaseInfoMapper tkTaskCaseInfoMapper;
    @Autowired
    private TkTaskCaseMapper tkTaskCaseMapper;

    /**
     * 查询
     *
     * @param id 主键
     * @return
     */
    @Override
    public TkTaskCaseInfo selectTkTaskCaseInfoById(String id) {
        return tkTaskCaseInfoMapper.selectTkTaskCaseInfoById(id);
    }

    /**
     * 查询列表
     *
     * @param tkTaskCaseInfo
     * @return
     */
    @Override
    public List<TkTaskCaseInfoVo> selectTkTaskCaseInfoList(String caseId) {
        List<TkTaskCaseInfo> tkTaskCaseInfoList = tkTaskCaseInfoMapper.selectTkTaskCaseInfoList(caseId);
       /* System.out.println(tkTaskCaseInfoList);
        List<TkTaskCaseInfoVo> tkTaskCaseInfoVoList = new ArrayList<>();
        for (TkTaskCaseInfo info : tkTaskCaseInfoList) {
            TkTaskCaseInfoVo vo = new TkTaskCaseInfoVo();
            String execContent = info.getExecContent();
            JSONObject jsonObject = JSONObject.parseObject(execContent);

            *//*String execLorData = (String) jsonObject.get("execLorData");
            if (JSONUtils.isJSONString(execLorData)) {
                JSONObject execLorDataJson = JSONObject.parseObject(execLorData);
                jsonObject.put("execLorData", execLorDataJson);
            }*//*


            vo.setId(info.getId());
            vo.setCaseId(info.getCaseId());
            vo.setRemark(info.getRemark());
            vo.setExecResult(info.getExecResult());
            vo.setExecContent(jsonObject);
            tkTaskCaseInfoVoList.add(vo);
        }*/


        List<TkTaskCaseInfoVo> collect = tkTaskCaseInfoList.stream().map(item -> {
            TkTaskCaseInfoVo tkTaskCaseInfoVo = new TkTaskCaseInfoVo();
            BeanUtils.copyProperties(item, tkTaskCaseInfoVo);
            tkTaskCaseInfoVo.setExecContent(JSONObject.parseObject(item.getExecContent()));
            return tkTaskCaseInfoVo;
        }).collect(Collectors.toList());


        return collect;
    }

    /**
     * 新增
     *
     * @param tkTaskCaseInfo
     * @return 结果
     */
    @Override
    public int insertTkTaskCaseInfo(TkTaskCaseInfo tkTaskCaseInfo) {
        Long caseInfoTotal = (long) (getCaseInfoTotal(tkTaskCaseInfo.getCaseId()) + 1);
        TkTaskCase tkTaskCase = new TkTaskCase();
        tkTaskCase.setCaseTotal(caseInfoTotal);
        tkTaskCase.setId(tkTaskCaseInfo.getCaseId());
        tkTaskCaseMapper.updateTkTaskCase(tkTaskCase);
        return tkTaskCaseInfoMapper.insertTkTaskCaseInfo(tkTaskCaseInfo);
    }

    /**
     * 修改
     *
     * @param
     * @return 结果
     */
    @Override
    public int updateTkTaskCaseInfo(TkTaskCaseInfoVo infoVo) {
        TkTaskCaseInfo tkTaskCaseInfo = new TkTaskCaseInfo();
        BeanUtils.copyProperties(infoVo, tkTaskCaseInfo);
        Map<String, Object> execContent = infoVo.getExecContent();
        Object execLorData = execContent.get("execLorData");
        if (execLorData != null && JSONUtils.isJSONString(execLorData.toString())) {
            JSONObject jsonObject = JSONObject.parseObject(execLorData.toString());
            execContent.put("execLorData", jsonObject);
        }
        tkTaskCaseInfo.setExecContent(JSON.toJSONString(execContent));
        if (null == tkTaskCaseInfo.getId()) {
            return insertTkTaskCaseInfo(tkTaskCaseInfo);
        }
        return tkTaskCaseInfoMapper.updateTkTaskCaseInfo(tkTaskCaseInfo);
    }

    /**
     * 批量删除
     *
     * @param ids 需要删除的主键
     * @return 结果
     */
    @Override
    public int deleteTkTaskCaseInfoByIds(String[] ids) {
        return tkTaskCaseInfoMapper.deleteTkTaskCaseInfoByIds(ids);
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return 结果
     */
    @Override
    public int deleteTkTaskCaseInfoById(String id) {
        return tkTaskCaseInfoMapper.deleteTkTaskCaseInfoById(id);
    }

    /**
     * 获取用例详情总条数
     *
     * @param caseId
     * @return
     */
    public int getCaseInfoTotal(String caseId) {
        return tkTaskCaseInfoMapper.getCaseInfoTotal(caseId);
    }
}
